package com.suning.sawp.in.controller.coupon;

import java.io.OutputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.constants.PromotionErrorConstants;
import com.suning.sawp.dto.common.QueryResult;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.precharge.CommonRtn;
import com.suning.sawp.dto.promotion.CouponRecordStatisRtn;
import com.suning.sawp.dto.promotion.InnerCouponBlackList;
import com.suning.sawp.dto.promotion.QueryStaffName;
import com.suning.sawp.in.controller.DjBaseController;
import com.suning.sawp.in.controller.MyGsonView;
import com.suning.sawp.intf.promotion.InnerCouponService;

/**
 * 
 * 内部券后台管理<br>
 * 〈功能详细描述〉
 * 
 * @author Wuxiao
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@RequestMapping("/adminPage/innerCoupon")
@Controller
public class InnerCouponManageController extends DjBaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstants.SAWP_SERVICE);

    @Resource
    InnerCouponService          innerCouponService;

    /**
     * 
     * 功能描述: 黑名单管理<br>
     * 〈功能详细描述〉
     * 
     * @param staffId
     * @param pageNo
     * @param pageSize
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryBlackList.do")
    public MyGsonView queryBlackList(String staffId, Integer pageNo, Integer pageSize) {
        ReturnMsg<QueryResult<InnerCouponBlackList>> ret = new ReturnMsg<QueryResult<InnerCouponBlackList>>();
        try {
            ret = innerCouponService.queryBlackList(staffId, pageNo, pageSize);
        } catch (Exception e) {
            ret.setError(PromotionErrorConstants.INNER_COUPON_ERR_E999,
                    PromotionErrorConstants.INNER_COUPON_ERR_MAP);
            LOGGER.error(String.format("后台查询黑名单列表,工号:%s,", staffId), e);
        }
        return setGsonView(ret);
    }

    /**
     * 
     * 功能描述:增加黑名单 <br>
     * 〈功能详细描述〉
     * 
     * @param staffId
     * @param staffName
     * @param reason
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/addBlack.do")
    public MyGsonView addBlack(String staffId, String staffName, String reason) {
        ReturnMsg<CommonRtn> ret = new ReturnMsg<CommonRtn>();
        try {
            ret = innerCouponService.addBlack(staffId, staffName, reason);
        } catch (Exception e) {
            ret.setError(PromotionErrorConstants.INNER_COUPON_ERR_E999,
                    PromotionErrorConstants.INNER_COUPON_ERR_MAP);
            LOGGER.error(String.format("后台新增黑名单失败,工号:%s,姓名：%s,原因:%s", staffId, staffName, reason),
                    e);
        }
        return setGsonView(ret);
    }

    /**
     * 
     * 功能描述: 删除黑名单<br>
     * 〈功能详细描述〉
     * 
     * @param staffId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/deleteBlack.do")
    public MyGsonView deleteBlack(String staffId) {
        ReturnMsg<CommonRtn> ret = new ReturnMsg<CommonRtn>();
        try {
            ret = innerCouponService.delBlack(staffId);
        } catch (Exception e) {
            ret.setError(PromotionErrorConstants.INNER_COUPON_ERR_E999,
                    PromotionErrorConstants.INNER_COUPON_ERR_MAP);
            LOGGER.error(String.format("删除黑名单失败,工号:%s", staffId), e);
        }
        return setGsonView(ret);
    }

    /**
     * 
     * 功能描述:根据工号查询姓名 <br>
     * 〈功能详细描述〉
     * 
     * @param staffId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryStaffName.do")
    public MyGsonView queryStaffName(String staffId) {
        ReturnMsg<QueryStaffName> ret = new ReturnMsg<QueryStaffName>();
        try {
            ret = innerCouponService.queryStaffName(staffId);
        } catch (Exception e) {
            ret.setError(PromotionErrorConstants.INNER_COUPON_ERR_E999,
                    PromotionErrorConstants.INNER_COUPON_ERR_MAP);
            LOGGER.error(String.format("黑名单查询员工姓名失败,工号:%s", staffId), e);
        }
        return setGsonView(ret);
    }

    /**
     * 
     * 功能描述:查询发券记录统计<br>
     * 〈功能详细描述〉
     * 
     * @param staffId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/queryRecordList.do")
    public MyGsonView queryRecordList(String staffId, String startTime, String endTime,
            Integer pageSize, Integer pageNo) {
        ReturnMsg<CouponRecordStatisRtn> ret = new ReturnMsg<CouponRecordStatisRtn>();
        try {
            ret = innerCouponService.queryRecord(staffId, startTime, endTime, pageSize, pageNo);
        } catch (Exception e) {
            ret.setError(PromotionErrorConstants.INNER_COUPON_ERR_E999,
                    PromotionErrorConstants.INNER_COUPON_ERR_MAP);
            LOGGER.error(String.format("后台查询发全记录统计失败,工号:%s,开始时间:%s,结束时间:%s", staffId, startTime,
                    endTime), e);
        }
        return setGsonView(ret);
    }

    /**
     * 
     * 功能描述: 导出发券记录明细<br>
     * 〈功能详细描述〉
     *
     * @param staffId
     * @param startTime
     * @param endTime
     * @param index
     * @param response
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/exportRecord.do")
    public void exportRecord(String staffId, String startTime, String endTime, Integer index,
            HttpServletResponse response) {
        OutputStream ouputStream = null;
        try {
            index = index == null ? 0 : index;
            Workbook workbook = innerCouponService.exportCouponRecord(staffId, startTime, endTime,
                    index);
            if(workbook == null){
                response.setContentType("text/html; charset=UTF-8");
                response.setHeader("Cache-Control", "no-cache");
                ouputStream = response.getOutputStream();
                ouputStream.write(PromotionErrorConstants.INNER_COUPON_ERR_MAP.get(PromotionErrorConstants.INNER_COUPON_ERR_E009).getBytes());
            }else{
                response.setContentType("application/x-download");
                response.setHeader("Content-disposition", "attachment;filename=innerCouponRecord_" + index
                        + ".xlsx");
                ouputStream = response.getOutputStream();
                workbook.write(ouputStream);
            }
            ouputStream.flush();
            ouputStream.close();
        } catch (Exception e) {
            LOGGER.error("exportRecord 导出excel出错", e);
        }
    }

}
